using System;
using System.Data;

namespace FreeFlow.Administration
{
	/// <summary>
	/// A user session.
	/// </summary>
	public class Session : DatabaseAware, IDeletable
	{
		internal Session(Server server, IDataReader reader) : base(server)
		{
      sessionId = reader["eSessionID"].ToString();
      userName = reader["eUserName"].ToString();
      lastActivity = (DateTime)reader["eLastActivityTime"];
      callerId = reader["eCallerID"].ToString();
      if (reader["eSessionTimeout"] != System.DBNull.Value)
        timeout = Convert.ToInt32(reader["eSessionTimeout"]);
		}

    private int timeout;
    /// <summary>
    /// Gets the period of inactivity after which the session will expire (in minutes).
    /// </summary>
    public int Timeout
    {
      get { return timeout; }
    }

    private string sessionId;
    /// <summary>
    /// Gets the ID for this session.
    /// </summary>
    public string SessionId
    {
      get
      {
        return sessionId;
      }
    }

    private string userName;
    /// <summary>
    /// Gets the name of the user for this session.
    /// </summary>
    public string UserName
    {
      get
      {
        return userName;
      }
    }

    private DateTime lastActivity;
    /// <summary>
    /// Gets the last activity for this session.
    /// </summary>
    public DateTime LastActivity
    {
      get
      {
        return lastActivity;
      }
    }

    private string callerId;
    /// <summary>
    /// Gets the valid caller ID for this session.
    /// </summary>
    public string CallerId
    {
      get
      {
        return callerId;
      }
    }

    #region IDeletable Members

    /// <summary>
    /// Deletes this session.
    /// </summary>
    public void Delete()
    {
      Server.ExecuteNonQuery(string.Format("DELETE FROM eSession WHERE eSessionID='{0}'", SqlEscape(sessionId)));
    }

    #endregion
  }
}
